<div class="row">
    <div class="col-sm-12">
        <div class="box">
            <div class="box-header with-border">
                <h3 class="page-header"><i class="fa fa-edit"></i> <?php echo $site["name"];?> 配置修改</h3>
            </div>
            <div class="box-body">


            <div class="form-group col-sm-12">
                    <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-2 control-label')).'>域名</label>';?>
                    <div class="col-sm-8" shopid="<?php echo $site["id"]; ?>"> 
                        <span style="line-height:20px;" ondblclick="edit(this,'domian',1)"><?php echo $site["domian"]?$site["domian"]:"双击设置";?></span>
                    </div>
                </div>

            <div class="form-group col-sm-12">
                    <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-2 control-label')).'>API密钥</label>';?>
                    <div class="col-sm-8" shopid="<?php echo $site["id"]; ?>"> 
                        <span style="line-height:20px;" ondblclick="edit(this,'apipass',1)"><?php echo $site["apipass"]?$site["apipass"]:"双击设置";?></span>
                    </div>
                </div>


          

                <div class="form-group col-sm-12">
                    <?php  echo '<label for="url"'._stringify_attributes(array('class' => 'col-sm-2 control-label')).'>产品页模板</label>';?>
                    <div class="col-sm-8" shopid="<?php echo $site["id"]; ?>"> 
                        <textarea name="goodtmpl" id="goodtmpl" class="form-control input-sm" style="height:120px;"><?php echo isset($site["extra"]["goodtmpl"])?$site["extra"]["goodtmpl"]:"";?></textarea>
                        <button class="btn btn-primary btn-xs btn-flat" id="updateauth">更新模板</button>
                    </div>
                </div>



            </div>
        </div>
    </div>
</div>

<script>
    atedit=0;
    function edit(obj,attrName,attrtype=0){
        if (atedit==1)
        return;
        else
        atedit=1;

        var oldHtml = obj.innerHTML;
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class","form-control input-sm");
        if($.trim(oldHtml)!="双击设置"){
            newobj.value = oldHtml;
        }

        var cid = $(obj).parent().attr("shopid");     
        obj.innerHTML = '';  
        obj.appendChild(newobj);
        newobj.focus(); 

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        if(attrtype==1){
            eurl="/backend/sites/site/ajaxedit";
        }else{
            eurl="/backend/sites/site/ajaxextraedit";
        }
        $(newobj).blur(function(){
            atedit=0;
            if(this.value==null || this.value==""){
                obj.innerHTML = oldHtml;
                return false;
            }
            obj.innerHTML = this.value?this.value:oldHtml;       
            if(oldHtml != this.value){
                $.ajax({
                    type:"post",
                    url:eurl,
                    dataType:"json",
                    data:{"attrName":attrName,"newHtml":this.value,"cid":cid},
                    error:function(){
                    },
                    success:function(){
                        layer.msg("更新成功");
                    },
                });
            }  

        })
    }

    function test(idname)
    {
        var ruler=$("#"+idname).text();
        layer.open({
            id:1,
            type: 1,
            title:'测试价格公式',
            skin:'layui-layer-rim',
            area:['450px', 'auto'],
            
            content: ' <div class="row" style="width: 420px;  margin-left:7px; margin-top:10px;">'
                +'<div class="col-sm-12">'
                +'<div class="input-group">'
                +'<span class="input-group-addon">输入目标价格:</span>'
                +'<input id="testprice" type="text" class="form-control" placeholder="请输入价格">'
                +'</div>'
                +'</div>'
                +'<div class="col-sm-12" style="margin-top: 10px">'
                +'<div class="input-group">'             
                +ruler
                +'</div>'
                +'</div>'
                +'</div>'
            ,
            btn:['测试'],
            btn1: function (index,layero) {
                var price=$("#testprice").val();
                if(price.length==0)
                {
                    layer.msg("价格不能为空");
                }
                $.ajax({
                    type:"post",
                    url:"/backend/sites/site/testprice",
                    dataType:"json",
                    data:{"ruler":ruler,"price":price},
                    error:function(){
                    },
                    success:function(data){
                        layer.msg("最终价格:"+data.price);
                    },
                });
            },    
        });
    }

    $("button#updateauth").on("click",function(){
        var authval=$("#goodtmpl").val();
        var cid=$(this).parent().attr('shopid');
        eurl="/backend/sites/site/ajaxextraedit";
        $.ajax({
            type:"post",
            url:eurl,
            dataType:"json",
            data:{"attrName":"goodtmpl","newHtml":authval,"cid":cid},
            error:function(){
            },
            success:function(){
                layer.msg("产品模板更新成功");
            },
        });
    })

</script>
